import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;

class HttpDemoPage extends StatefulWidget {
  HttpDemoPage({Key key}) : super(key: key);

  @override
  _HttpDemoPageState createState() => _HttpDemoPageState();
}

class _HttpDemoPageState extends State<HttpDemoPage> {
  List mList = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getData();
  }

  _getData() async {
    var url = "http://a.itying.com/api/productlist";
    var reslut = await http.get(url);
    if (reslut.statusCode == 200) {
      setState(() {
        this.mList = json.decode(reslut.body)["result"];
      });
    } else {
      print("失败${reslut.statusCode}");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("网络请求Demo"),
      ),
      body: this.mList.length > 0
          ? ListView.builder(
              itemCount: this.mList.length,
              itemBuilder: (context, index) {
                return ListTile(
                  title: Text(this.mList[index]["title"]),
                );
              },
            )
          : Text("加载中..."),
    );
  }
}
